package eight.spring.jmx;

import java.util.Iterator;
import java.util.Set;

import javax.annotation.Resource;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@SuppressWarnings({"unused","rawtypes"})
public class RemoteClient {
	
	private  MBeanServerConnection mBeanServerConnection;
	
	public static void main(String[] args) throws Exception {
		ApplicationContext ac = new ClassPathXmlApplicationContext("eight/spring/jmx/beans4.xml");
		RemoteClient remoteClient = (RemoteClient)ac.getBean("remoteClient");
		MBeanServerConnection serverConnection = remoteClient.mBeanServerConnection;
		//获取服务器中注册的MBean的数量
		int mbeanCount = serverConnection.getMBeanCount();
		System.out.println(mbeanCount);
		//查看所有mbean名称
		Set mbeanNames = serverConnection.queryNames(null, null);
		for(Iterator it = mbeanNames.iterator(); it.hasNext();){
			System.out.println(it.next().toString());
		}
		//获取MBean中被管理的属性的值
		String value = serverConnection.getAttribute(new ObjectName("liuys:name=testBean3"), "Name").toString();
		System.out.println("name=" + value);
		//调用MBean被管理的方法
		serverConnection.invoke(new ObjectName("liuys:name=testBean3"), "add", new Integer[]{30,20}, new String[]{"int","int"});
	}

	public void setmBeanServerConnection(MBeanServerConnection mBeanServerConnection) {
		this.mBeanServerConnection = mBeanServerConnection;
	}
	
}
